package sort;

/**
 * @Description: null
 * @Project: testDemo
 * @Author: hjibo
 * @CreateDate: 2019-02-12 18:29
 * @ModificationHistory:
 */
public class Main {

    public static void main(String[] args) {
        int[] arr = new int[]{1,3,5,7,8,9,10};
        System.out.println(searchFirstLargeThanThisValue(arr,4));
    }
    // 二分法查找第一个大于目标值得索引位置
    public static int searchFirstLargeThanThisValue(int[] arr, int value) {
        int L = 0,
            R = arr.length - 1,
            M = -1;
        while (L <= R) {
            M = (L + R) / 2;
            if(L == R){
                return  M;
            }
            if (arr[M] <= value) {
                L = M +1;
                continue;
            }
            if (arr[M] > value) {
                R = M;
            }
        }
        return M;
    }
}
